package com.yh.serviceimpl;

import com.yh.dao.PermissionDao;
import com.yh.dao.RoleDao;
import com.yh.dao.UsersDao;
import com.yh.dao.UsersRoleDao;
import com.yh.javabean.Permission;
import com.yh.javabean.Role;
import com.yh.javabean.Users;
import com.yh.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("usersService")
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersDao usersDao;

    @Autowired
    RoleDao roleDao;

    @Autowired
    UsersRoleDao usersRoleDao;

    @Autowired
    PermissionDao permissionDao;


    //如果没有查询到对应数据 返回null(最好手动抛出异常)
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询数据返回实体类对应数据
        Users users = usersDao.selectByUserName(username);
        if(users!=null){

            //使用明文密码登录
            User user = new User(users.getUsername(),
                    "{noop}"+users.getPassword(),
                    users.getStatus() == 0 ? false : true ,
                    true,true,true,
                    getAuthority(users.getId()));
            return user;
        }else {
            throw new UsernameNotFoundException("账号不存在");
        }
    }

    //默认users中没有任何权限 无法登录

    public Set<GrantedAuthority> getAuthority(int uid){
        Set<GrantedAuthority> simpleGrantedAuthorities = new HashSet<>();

        List<Permission> list = permissionDao.selectByUid(uid);
        if(list!=null){
            for (Permission permission : list) {
                simpleGrantedAuthorities.add(new SimpleGrantedAuthority(permission.getPermissionName()));
            }
        }

//        Role role = roleDao.selectByUid(uid);
//        simpleGrantedAuthorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));

        return  simpleGrantedAuthorities;
    }

    //查询用户列表
    @Override
    public List<Users> findAll() {
        return usersDao.selectAll();
    }

    @Override
    public List<Role> findAllRole() {
        return roleDao.selectAll();
    }

    @Override
    @Transactional
    public void addUsers(Users users, int roleId) {
        usersDao.insertUsers(users);
        usersRoleDao.insertUR(users.getId(),roleId);
    }

  //删除用户
    @Override
    @Transactional
    public void deleteUsers(int id) {
        usersDao.deleteById(id);
        usersRoleDao.deleteByUid(id);
    }

    @Override
    public void editUsers(Users users, int roleId) {
        usersDao.updateUsers(users);
        usersRoleDao.UpdateRoleIdByUsersId(users.getId(),roleId);
    }

    @Override
    public Users findOne(int id) {
        return usersDao.selectById(id);
    }
}
